<template>
  <div>
    <uploadExcel :on-success="onSuccess" />
  </div>
</template>

<script>
import { batchEmployees } from '@/api/employees'
export default {
  methods: {
    async onSuccess(data) {
      // console.log(data)
      const newData = data.results.map(user => {
        return this.zhToEn(user)
      })
      // console.log(newData)
      await batchEmployees(newData)
      this.$message.success('上传成功')
      this.$router.back()
    },
    zhToEn(objUser) {
      const arrUser = {}
      const zidian = {
        入职日期: 'timeOfEntry',
        姓名: 'username',
        工号: 'workNumber',
        手机号: 'mobile',
        转正日期: 'correctionTime'
      }
      for (const key in objUser) {
        const enkey = zidian[key]
        const value = objUser[key]
        if (enkey === 'timeOfEntry' || enkey === 'correctionTime') {
          arrUser[enkey] = new Date(this.formatExcelTime(value))
        } else {
          arrUser[enkey] = value
        }
      }
      return arrUser
    },
    // 参考: https://www.tqwba.com/x_d/jishu/352922.html
    formatExcelTime(num, format = '-') {
      num = Number(num)	// 强制类型转化，以防传来的值是字符串
      let millisecond = 0	// 转化后的毫秒数
      if (num > 60) {
        millisecond = (num - 25569) * 60 * 60 * 24 * 1000
      } else {
        millisecond = (num - 25568) * 60 * 60 * 24 * 1000
      }
      const date = new Date(millisecond)	// 根据转化后的毫秒数获取对应的时间
      const yy = date.getFullYear()
      const mm = date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1
      const dd = date.getDate() < 10 ? '0' + date.getDate() : date.getDate()
      return yy + format + mm + format + dd	// 返回格式化后的日期
    }
  }
}
</script>

<style>

</style>
